﻿using System;
using System.Collections.Generic;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Text;

using Model;
using Model.Components;
using FrameWork.Components;


namespace FrameWork.web.Manager.Module.Tousu.Minyi
{
    public partial class Default : System.Web.UI.Page
    {
        string ZT = (string)Common.sink("ZT", MethodType.Get, 10, 0, DataType.Str);
        public Int32 CS = (Int32)Common.sink("CS", MethodType.Get, 10, 0, DataType.Int);

        protected void Page_Load(object sender, EventArgs e)
        {
            if (!Page.IsPostBack)
            {
                BindDataList();
            }
        }

        /// <summary> 
        /// 绑定数据
        /// </summary>
        private void BindDataList()
        {
            StringBuilder sb = new StringBuilder();
            sb.Append("where  Type='来民' ");
            QueryParam qp = new QueryParam();
            if (FrameWorkPermission.CheckButtonPermission(PopedomType.Orderby))// 民民办
            {
                if (ZT == "未处理")//未处理 包括未处理和退回 所有
                {
                    sb.AppendFormat(" AND zhuangtai = '未处理' or zhuangtai='退回'", ZT);
                }
                if (ZT == "办结")//办结  状态为办结
                {
                    sb.AppendFormat(" AND zhuangtai = '办结'  ", ZT);
                }
                if (ZT == "办理中") //超时  当前日期大于办结日期  状态为办理中的
                {
                    sb.AppendFormat(" AND zhuangtai = '办理中'  and getdate()>= BEndDate", ZT);
                }
            }

            if (FrameWorkPermission.CheckButtonPermission(PopedomType.Print))//局长
            {
                sb.AppendFormat(" and toLingDao='{0}'", UserData.GetUserDate.UserID.ToString());
                if (ZT == "领导审批")//未处理  提交领导为当前局长id 状态为领导审批
                {
                    sb.AppendFormat(" AND zhuangtai = '领导审批' ");
                }
                if (ZT == "办结")//办结 提交领导为当前局长id 状态为办结
                {
                    sb.AppendFormat(" AND zhuangtai = '办结' ");
                }
                if (ZT == "办理中")//超时 提交领导为当前局长id 状态为办理中 时间大于办理时间
                {
                    sb.AppendFormat(" AND zhuangtai = '办理中' and  getdate()>= BEndDate ");
                }

            }

            if (FrameWorkPermission.CheckButtonPermission(PopedomType.A))//县局
            {
                sb.AppendFormat(" and  BChuliGroup='{0}' ", UserData.GetUserDate.U_GroupID.ToString());
                if (ZT == "办理中")
                {
                    if (CS == 1) //时间超时  状态为办理中 时间超时 切单位为本单位
                    {
                        sb.AppendFormat(" AND {0}>= BEndDate  and zhuangtai ='办理中' ", "getdate()");
                    }
                    else //未办理 状态为未办理
                    {
                        sb.AppendFormat(" AND zhuangtai = '办理中' ", ZT);
                    }
                }
                if (ZT == "办结")
                {
                    sb.AppendFormat("   and zhuangtai ='办结' ");
                }
            }
            qp.Where = sb.ToString();
            qp.PageIndex = AspNetPager1.CurrentPageIndex;
            qp.PageSize = AspNetPager1.PageSize;
            qp.Orderfld = Orderfld;
            qp.OrderType = OrderType;
            int RecordCount = 0;
            List<T_TouSuEntity> lst = BusinessFacadeModel.T_TouSuList(qp, out RecordCount);
            GridView1.DataSource = lst;
            GridView1.DataBind();
            this.AspNetPager1.RecordCount = RecordCount;
        }

        /// <summary>
        /// 点击分页事件
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        protected void AspNetPager1_PageChanged(object sender, EventArgs e)
        {
            BindDataList();
        }

        /// <summary>
        /// 查询条件
        /// </summary>
        private string SearchTerms
        {
            get
            {
                if (ViewState["SearchTerms"] == null)
                    ViewState["SearchTerms"] = " Where 1=1 ";
                return (string)ViewState["SearchTerms"];
            }
            set { ViewState["SearchTerms"] = value; }
        }



        #region "排序"
        protected void GridView1_Sorting(object sender, GridViewSortEventArgs e)
        {
            if (Orderfld == e.SortExpression)
            {
                if (OrderType == 0)
                {
                    OrderType = 1;
                }
                else
                {
                    OrderType = 0;
                }
            }
            Orderfld = e.SortExpression;
            BindDataList();
        }

        /// <summary>
        /// 排序字段
        /// </summary>
        public string Orderfld
        {
            get
            {
                if (ViewState["sortOrderfld"] == null)

                    ViewState["sortOrderfld"] = "ID";

                return (string)ViewState["sortOrderfld"];
            }
            set
            {
                ViewState["sortOrderfld"] = value;
            }
        }

        /// <summary>
        /// 排序类型 1:降序 0:升序
        /// </summary>
        public int OrderType
        {

            get
            {

                if (ViewState["sortOrderType"] == null)
                    ViewState["sortOrderType"] = 1;

                return (int)ViewState["sortOrderType"];


            }

            set { ViewState["sortOrderType"] = value; }

        }
        /// <summary>
        /// 排序事件
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        protected void GridView1_RowCreated(object sender, GridViewRowEventArgs e)
        {
            Int32 DataIDX = 0;
            if (e.Row.RowType == DataControlRowType.Header)
            {
                if (FrameWorkPermission.CheckButtonPermission(PopedomType.Delete))
                {
                    Button2.Visible = true;
                    //增加check列头全选
                    TableCell cell = new TableCell();
                    cell.Width = Unit.Pixel(5);
                    cell.Text = " <input id='CheckboxAll' value='0' type='checkbox' onclick='SelectAll()'/>";
                    e.Row.Cells.AddAt(0, cell);
                }

                foreach (TableCell var in e.Row.Cells)
                {
                    if (var.Controls.Count > 0 && var.Controls[0] is LinkButton)
                    {
                        string Colume = ((LinkButton)var.Controls[0]).CommandArgument;
                        if (Colume == Orderfld)
                        {

                            LinkButton l = (LinkButton)var.Controls[0];
                            l.Text += string.Format("<img src='{0}' border='0'>", (OrderType == 0) ? Page.ResolveUrl("~/Manager/images/sort_asc.gif") : Page.ResolveUrl("~/Manager/images/sort_desc.gif"));

                        }
                    }
                }
            }
            else
            {
                if (FrameWorkPermission.CheckButtonPermission(PopedomType.Delete))
                {
                    //增加行选项
                    DataIDX = Convert.ToInt32(DataBinder.Eval(e.Row.DataItem, "ID"));
                    TableCell cell = new TableCell();
                    cell.Width = Unit.Pixel(5);
                    cell.Text = string.Format(" <input name='Checkbox' id='Checkbox' value='{0}' type='checkbox' />", DataIDX);
                    e.Row.Cells.AddAt(0, cell);
                }
            }
        }
        #endregion

        /// <summary>
        /// 删除
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        [PopedomTypeAttaible(PopedomType.Delete)]
        [System.Security.Permissions.PrincipalPermission(System.Security.Permissions.SecurityAction.Demand, Role = "OR")]
        protected void Button2_Click(object sender, EventArgs e)
        {
            string Checkbox_Value = (string)Common.sink("Checkbox", MethodType.Post, 2000, 1, DataType.Str);
            string[] Checkbox_Value_Array = Checkbox_Value.Split(',');
            Int32 IDX = 0;
            for (int i = 0; i < Checkbox_Value_Array.Length; i++)
            {
                if (Int32.TryParse(Checkbox_Value_Array[i], out IDX))
                {
                    T_TouSuEntity et = new T_TouSuEntity();
                    et.DataTable_Action_ = DataTable_Action.Delete;
                    et.ID = IDX;
                    BusinessFacadeModel.T_TouSuInsertUpdateDelete(et);
                }
            }

            EventMessage.MessageBox(1, "批量删除成功", string.Format("批量删除({0})成功!", Checkbox_Value), Icon_Type.OK, Common.GetHomeBaseUrl("default.aspx"));
        }



        public string Get_U_GroupID(int U_GroupID)
        {
            return BusinessFacade.sys_GroupDisp(U_GroupID).G_CName;
        }
    }
}